package ca.swank.hardquor;

/**
* Provides communication between a hardqour game server and an implementation
* of the <CODE>hardquorUserGame<CODE> interface. To use this class:
* <OL>
*   <LI>develop an implementation of the <CODE>hardquorUserGame</CODE> interface. The
*       implementation should take a <CODE>hardquorGameClient</CODE> instance as a
*       construction parameter, which will be used for communication to the hardquor
*       game server.
*   <LI>Respond to user actions in your <CODE>hardquorUserGame</CODE> implementation by
*       calling methods on the <CODE>hardquorClientGame</CODE> object.
*   <LI>Respond to methods of the <CODE>hardquorUserGame</CODE> interface with appropriate
*       actions.
* </OL>
*
* @author Jesse Wilson
* @version 1.0 9/23/2001
*/
public class hardquorGameClient
{
  private hardquorClient client;
  private int gameNumber;

  private hardquorUserGame userGame;

  /**
  * create a hardquorGameClient instance. This instance shares the connection with the
  * <CODE>hardquorClient</CODE> specified. When implementing the <CODE>hardquorUserGame</CODE>
  * or <CODE>hardquorUser</CODE> interfaces, do not create <CODE>hardquorGameClient</CODE>
  * instances; but use the instance passed as a parameter to the <CODE>startGame</CODE>
  * of the <CODE>hardquorUser</CODE> class.
  *
  * @param client the <CODE>hardquorClient</CODE> to share a connection to a hardquor
  *        server with.
  * @param gameNumber the game number provided by the hardquor game server for tracking
  *        moves from different hardquor games.
  */
  public hardquorGameClient(hardquorClient client, int gameNumber)
  { this.client = client;
    this.gameNumber = gameNumber;
  }

  /**
  * sets the <CODE>hardquorUserGame</CODE> instance that this object will notify
  * server commands to.
  *
  * @param usergame the <CODE>hardquorUserGame</CODE> instance to notify game commands to.
  */
  public void setUserGame(hardquorUserGame userGame)
  { this.userGame = userGame;
  }

  /**
  * sends a walk move request to the current hardquor game server. The server will
  * process the move command and the <CODE>notifyError</CODE> method of the
  * <CODE>hardquorUserGame</CODE> instance will be called with an error code to
  * classify the move as either successful move or in error.
  *
  * @param source a 2 integer array representing the initial coordinate of the current
  *        player's  token. Index 0 is the x location, index 1 is the y location for all
  *        coordinates.
  * @param target a 2 integer array representing the new coordinate of the current
  *        player's token.
  */
  public void tryMove(int[] source, int[] target)
  { client.writeln("GAMEMOVE " + gameNumber + " " + coord(source) + " " + coord(target));
  }

  /**
  * sends a wall move request to the current hardquor game server. The server will
  * process the move command and the <CODE>notifyError</CODE> method of the
  * <CODE>hardquorUserGame</CODE> instance will be called with an error code to
  * classify the move as either successful move or in error.
  *
  * @param a1 a 2 integer array representing the first of two cells on the A side of
  *        the new wall. This cell is no longer adjacent to <CODE>b1</CODE>.
  *        Index 0 is the x location, index 1 is the y location for all coordinates.
  * @param a2 a 2 integer array representing the second of two cells on the A side of
  *        the new wall. This cell is no longer adjacent to <CODE>b2</CODE>.
  * @param b1 a 2 integer array representing the first of two cells on the B side of
  *        the new wall. This cell is no longer adjacent to <CODE>a1</CODE>.
  * @param b2 a 2 integer array representing the second of two cells on the B side of
  *        the new wall. This cell is no longer adjacent to <CODE>a2</CODE>.
  */
  public void tryMove(int[] a1, int[] a2, int[] b1, int[] b2)
  { client.writeln("GAMEMOVE " + gameNumber + " " + coord(a1) + " " + coord(a2) + " " + coord(b1) + " " + coord(b2));
  }

  /**
  * quits the current game. Upon quitting the game, no other methods involving this
  * game should be called. To enable users to chat upon completion of a game, do not
  * quit the game until such is complete.
  */
  public void gameQuit()
  { client.writeln("GAMEQUIT " + gameNumber);
  }

  /**
  * chats a message to the other user in the current hardquor game. These
  * messages are only available to other persons playing the current game,
  * and no other users on the hardquor game server. The user making speak
  * commands is not annonymous.
  *
  * @param comment the message to be broadcast to others connected to the
  *        current hardquor game server.
  */
  public void speak(String comment)
  { client.writeln("SPEAK " + gameNumber + " " + comment);
  }

  private String coord(int[] position)
  { return position[0] + "," + position[1];
  }


}